package com.micro.ai.agents.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.agents.entity.Conversation;
import com.micro.ai.agents.service.ConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 会话控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/conversations")
@Tag(name = "会话管理", description = "会话的增删改查、关闭、归档等功能")
public class ConversationController {

    @Autowired
    private ConversationService conversationService;

    @PostMapping
    @Operation(summary = "创建会话", description = "创建新的会话")
    public ApiResponse<Conversation> create(@Valid @RequestBody Conversation conversation) {
        log.info("创建会话请求: agentId={}, userId={}", conversation.getAgentId(), conversation.getUserId());
        Conversation result = conversationService.create(conversation);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新会话", description = "更新会话信息")
    public ApiResponse<Conversation> update(
            @Parameter(description = "会话ID", required = true) @PathVariable String id,
            @Valid @RequestBody Conversation conversation) {
        Conversation result = conversationService.update(id, conversation);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除会话", description = "删除会话")
    public ApiResponse<Void> delete(
            @Parameter(description = "会话ID", required = true) @PathVariable String id) {
        conversationService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取会话详情", description = "根据ID获取会话详细信息")
    public ApiResponse<Conversation> getById(
            @Parameter(description = "会话ID", required = true) @PathVariable String id) {
        Conversation conversation = conversationService.getById(id);
        if (conversation == null) {
            return ApiResponse.error("A0002", "会话不存在");
        }
        return ApiResponse.success(conversation);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询会话", description = "支持多条件查询会话列表")
    public ApiResponse<Page<Conversation>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "智能体ID") @RequestParam(value = "agentId", required = false) String agentId,
            @Parameter(description = "用户ID") @RequestParam(value = "userId", required = false) String userId,
            @Parameter(description = "状态") @RequestParam(value = "status", required = false) String status) {
        Page<Conversation> page = conversationService.pageQuery(pageNum, pageSize, tenantId, agentId, userId, status);
        return ApiResponse.success(page);
    }

    @GetMapping("/by-user")
    @Operation(summary = "查询用户会话列表", description = "根据用户ID查询会话列表")
    public ApiResponse<List<Conversation>> listByUserId(
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") String userId,
            @Parameter(description = "状态") @RequestParam(value = "status", required = false) String status) {
        List<Conversation> list = conversationService.listByUserId(tenantId, userId, status);
        return ApiResponse.success(list);
    }

    @PutMapping("/{id}/close")
    @Operation(summary = "关闭会话", description = "关闭指定的会话")
    public ApiResponse<Void> close(
            @Parameter(description = "会话ID", required = true) @PathVariable String id) {
        conversationService.close(id);
        return ApiResponse.successVoid("关闭成功");
    }

    @PutMapping("/{id}/archive")
    @Operation(summary = "归档会话", description = "归档指定的会话")
    public ApiResponse<Void> archive(
            @Parameter(description = "会话ID", required = true) @PathVariable String id) {
        conversationService.archive(id);
        return ApiResponse.successVoid("归档成功");
    }
}

